package utils;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.view.View;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.List;

import adapter.fragment.FragmentPagerItems;
import student.andlei.com.student.R;
import widget.CommonIndicatorView;

public class ViewPagerHelper {


    /**
     * 配置指示条
     *
     * @param magicIndicator
     * @param viewPager
     * @param fragmentPagerItems
     */
    public static void configIndicator(MagicIndicator magicIndicator, final ViewPager viewPager, final FragmentPagerItems fragmentPagerItems) {
        CommonNavigator commonNavigator = new CommonNavigator(magicIndicator.getContext());
        commonNavigator.setScrollPivotX(0.8f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return fragmentPagerItems == null ? 0 : fragmentPagerItems.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new CommonIndicatorView(context, fragmentPagerItems.get(index).getTitle().toString());
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        net.lucode.hackware.magicindicator.ViewPagerHelper.bind(magicIndicator, viewPager);

    }

    /**
     * 配置指示条  满屏
     *
     * @param magicIndicator
     * @param viewPager
     * @param fragmentPagerItems
     */
    public static void configIndicatorFixed(MagicIndicator magicIndicator, final ViewPager viewPager, final FragmentPagerItems fragmentPagerItems) {

        CommonNavigator commonNavigator = new CommonNavigator(magicIndicator.getContext());
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return fragmentPagerItems == null ? 0 : fragmentPagerItems.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new CommonIndicatorView(context, fragmentPagerItems.get(index).getTitle().toString());
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_MATCH_EDGE);
                linePagerIndicator.setLineWidth(UIUtil.dip2px(context, 10));
                linePagerIndicator.setColors(context.getResources().getColor(R.color.homepage_titlefont));
                return linePagerIndicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        net.lucode.hackware.magicindicator.ViewPagerHelper.bind(magicIndicator, viewPager);

    }
}
